package com.cq.hd.system.service.impl;

import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.system.api.vo.HfRegionVo;
import com.cq.hd.system.api.vo.RegionVo;
import com.cq.hd.system.po.TbHfRegionsPo;
import com.cq.hd.system.mapper.TbHfRegionsMapper;
import com.cq.hd.system.service.TbHfRegionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 汇付省市区表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2023-12-25
 */
@Service
public class TbHfRegionsServiceImpl extends ServiceImpl<TbHfRegionsMapper, TbHfRegionsPo> implements TbHfRegionsService {

    public static Map<Long, HfRegionVo> provinceInfoMap = new HashMap<>();
    public static Map<Long, HfRegionVo> cityInfoMap = new HashMap<>();
    public static List<HfRegionVo> provinceInfoList = new ArrayList<>();
    public static List<HfRegionVo> cityInfoList = new ArrayList<>();
    public static List<HfRegionVo> countyInfoList = new ArrayList<>();

    @PostConstruct
    public void init() {
        provinceInfoList = baseMapper.listRegionByGrade(1);
        cityInfoList = baseMapper.listRegionByGrade(2);

        for (HfRegionVo regionVo : provinceInfoList) {
            Long provinceId = regionVo.getId();
            List<HfRegionVo> provinceRegions = regionVo.getRegions();

            for (HfRegionVo city : cityInfoList) {
                Long cityId = city.getId();
                List<HfRegionVo> cityRegions = city.getRegions();

                if (city.getParentId().equals(provinceId)) {
                    provinceRegions.add(city);
                }

                for (HfRegionVo county : countyInfoList) {
                    if (county.getParentId().equals(cityId)) {
                        cityRegions.add(county);
                    }

                }
            }
        }

        provinceInfoMap = provinceInfoList.stream().collect(Collectors.toMap(HfRegionVo::getId, a -> a, (k1, k2) -> k1));
        cityInfoMap = cityInfoList.stream().collect(Collectors.toMap(HfRegionVo::getId, a -> a, (k1, k2) -> k1));
    }

    @Override
    public List<HfRegionVo> listHfRegionById(Integer id) {
        return baseMapper.listHfRegion(id);
    }

    @Override
    public List<HfRegionVo> listAllRegion() {
        return provinceInfoList;
    }

    @Override
    public List<HfRegionVo> listHfRegionByCityNames(Set<String> cityNames) {
        List<HfRegionVo> hfRegionVos = baseMapper.listHfRegionByCityNames(cityNames);
        if (!CollectionUtils.isEmpty(hfRegionVos)) {
            List<Long> provinceIds = hfRegionVos.stream().map(HfRegionVo::getParentId).collect(Collectors.toList());
            List<HfRegionVo> hfProvinceRegionsVos = baseMapper.selectByIds(provinceIds);
            if (!CollectionUtils.isEmpty(hfProvinceRegionsVos)) {
                Map<Long, List<HfRegionVo>> hfRegionsPoMap = hfRegionVos.stream().collect(Collectors.groupingBy(HfRegionVo::getParentId));
                List<HfRegionVo> hfRegionVosResult = new ArrayList<>();
                for (HfRegionVo hfProvinceRegionVo : hfProvinceRegionsVos) {
                    HfRegionVo hfRegionVo = new HfRegionVo();
                    BeanUtils.copyProperties(hfProvinceRegionVo, hfRegionVo);
                    hfRegionVo.setRegions(hfRegionsPoMap.get(hfProvinceRegionVo.getId()));
                    hfRegionVosResult.add(hfRegionVo);
                }

                return hfRegionVosResult;
            }
        }

        return null;
    }
}
